using System.Collections.Generic;
using UnityEngine;

public class LookSetPicker : MonoBehaviour
{
	public UIScrollView view;

	public UITable table;

	public LookButton buttonPrefab;

	private List<LookButton> buttons = new List<LookButton>();

	private CustomisationConfig.LookSet mySet;

	private int mySetId;

	private CustomisationImportPreview previewController;

	public UILabel setName;

	private Dictionary<Persona.Gender, Persona> genderSpecificLooks = new Dictionary<Persona.Gender, Persona>();

	public int looksCount { get; private set; }

	public void Init(CustomisationConfig.LookSet set, int setId, CustomisationImportPreview preview)
	{
		mySet = set;
		mySetId = setId;
		previewController = preview;
		base.gameObject.SetActive(true);
		UITools.ChangeText(setName, set.lookSetName);
		CreateLookButtons(set);
	}

	private void CreateLookButtons(CustomisationConfig.LookSet set)
	{
		Persona myPersona = new Persona(previewController.gender);
		myPersona = SelectGender(myPersona, previewController.gender);
		CustomisationConfig.Look defaultLookForPersona = set.GetDefaultLookForPersona(myPersona);
		looksCount = 0;
		for (int i = 0; i < set.looks.Count; i++)
		{
			if (set.looks[i].IsAvailable(myPersona))
			{
				while (looksCount >= buttons.Count)
				{
					LookButton component = NGUITools.AddChild(table.gameObject, buttonPrefab.gameObject).GetComponent<LookButton>();
					component.gameObject.name = GGFormat.IndexName(i);
					buttons.Add(component);
				}
				LookButton lookButton = buttons[looksCount];
				lookButton.Init(set.looks[i], i, this);
				if (set.looks[i] == defaultLookForPersona)
				{
					lookButton.OnSelectLook();
				}
				else
				{
					lookButton.UpdateLookSelection(false);
				}
				looksCount++;
			}
		}
		for (int j = looksCount; j < buttons.Count; j++)
		{
			buttons[j].gameObject.SetActive(false);
		}
		table.Reposition();
	}

	private void SaveGenderSpecificLooks(Persona myPersona)
	{
		genderSpecificLooks[myPersona.gender] = myPersona;
	}

	private Persona SelectGender(Persona myPersona, Persona.Gender newGender)
	{
		Persona persona = new Persona(newGender);
		Persona persona2 = null;
		if (genderSpecificLooks.ContainsKey(newGender))
		{
			persona2 = genderSpecificLooks[newGender];
		}
		foreach (Persona.LookSelection item in myPersona.myConfig)
		{
			if (item.selectedLook != null && item.selectedLook.IsAvailable(persona))
			{
				continue;
			}
			if (item.selectedLook != null)
			{
				CustomisationConfig.Look similarGenderLook = GetSimilarGenderLook(item.selectedLook.lookName, newGender, item.type);
				if (similarGenderLook != null)
				{
					persona.ChangeLook(similarGenderLook);
					continue;
				}
			}
			if (persona2 == null)
			{
				continue;
			}
			foreach (Persona.LookSelection item2 in persona2.myConfig)
			{
				if (item.type == item2.type && item2.selectedLook != null && item2.selectedLook.IsAvailable(persona))
				{
					persona.ChangeLook(item2.selectedLook);
					break;
				}
			}
		}
		myPersona = persona;
		return myPersona;
	}

	private CustomisationConfig.Look GetSimilarGenderLook(string name, Persona.Gender gender, CustomisationConfig.LookSetType type)
	{
		if (type == CustomisationConfig.LookSetType.Hair)
		{
			return null;
		}
		CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(type);
		return lookSetForType.GetLookByName(name, gender);
	}

	public void LookSelected(CustomisationConfig.Look look)
	{
		previewController.ApplySelectedLook(look, mySetId);
		foreach (LookButton button in buttons)
		{
			button.UpdateLookSelection(false);
		}
	}

	private void Update()
	{
		if (mySet != null)
		{
			UITools.ChangeText(setName, mySet.lookSetName);
		}
	}
}
